#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

int set_fl(int fd, int flags) {
	int val;
	if ((val = fcntl(fd, F_GETFL,0)) < 0) {
		printf("fcntl getfl error: %s\n",strerror(errno));
		return -1;
	}

	// turn on  flags
	val |= flags;
	// turn off flags
	 val &= ~flags;

	if (fcntl(fd, F_SETFL, val) < 0){
		printf("fcntl setfl error: %s\n",strerror(errno));
		return -1;
	}
	
	return 0;
}

#define BUFFSIZE 4096

int main() {
	int n;
	char buf[BUFFSIZE];

	set_fl(STDOUT_FILENO, O_SYNC);
	while ((n=read(STDIN_FILENO,buf,BUFFSIZE)) > 0) 
		if (write(STDOUT_FILENO,buf,n) != n) {
			printf("write error: %s\n",strerror(errno));
			return -1;
		}

	if (n<0)	{
		printf("read error: %s\n",strerror(errno));
		return -1;
	}

	return 0;
}
